<template>
    <div>
        <!--面包屑导航区域-->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/manufacturerIndex' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>货物管理</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图区域-->
        <el-card>
            <!--搜索与添加区域-->
            <el-row :gutter="20">
                <el-col :span="7">
                    <el-input placeholder="请输入查询信息" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加货物</el-button>
                </el-col>
            </el-row>

            <!--货物列表区域-->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="货物名称">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.goodsName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="货物数量" prop="goodsNum"></el-table-column>
                <el-table-column label="货物价格" prop="price"></el-table-column>
                <el-table-column label="货物描述" prop="description" :show-overflow-tooltip='true'></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
                            <el-button type="primary" size="mini" icon="el-icon-edit"
                                       @click="showEditDialog(scope.row.goodsId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-delete"
                                       @click="removeManufacturer(scope.row.goodsId)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>

            <!--分页区域-->
            <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.current"
                    :page-sizes="[2, 3, 5, 10]"
                    :page-size="queryInfo.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>

        <!--添加货物的对话框-->
        <el-dialog title="添加货物" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
            <!--内容主体区域-->
            <el-form ref="addGoodRef" :model="addGood" :rules="addGoodRules" label-width="70px">
                <el-form-item label="名称" prop="goodsName">
                    <el-input v-model="addGood.goodsName"></el-input>
                </el-form-item>
                <el-form-item label="数量" prop="goodsNum">
                    <el-input v-model.number="addGood.goodsNum"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model.number="addGood.price"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="addGood.description"></el-input>
                </el-form-item>
            </el-form>
            <!--底部区域-->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOneGood">确 定</el-button>
            </span>
        </el-dialog>

        <!--修改货物的对话框-->
        <el-dialog title="修改货物" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
            <el-form ref="editGoodRef" :model="editGood" :rules="editGoodRules"
                     label-width="70px">
                <el-form-item label="名称" prop="goodsNum">
                    <el-input v-model="editGood.goodsName"></el-input>
                </el-form-item>
                <el-form-item label="数量" prop="goodsNum">
                    <el-input v-model.number="editGood.goodsNum"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model.number="editGood.price"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="contact">
                    <el-input v-model="editGood.description"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editOneGood">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script scoped>
    export default {
        data() {
            var checkNumber = (rule, value, callback) => {
                if (!Number.isInteger(value)) {
                    callback(new Error('请输入数字值'));
                } else {
                    if (value < 0) {
                        callback(new Error('必须大于等于0'));
                    } else {
                        callback();
                    }
                }
            }
            return {
                // 获取用户列表的参数对象
                queryInfo: {
                    manufacturerId: '',
                    query: '',
                    // 当前的页数
                    current: 1,
                    // 当前每页显示多少条数据
                    pageSize: 7
                },
                goodsList: [],
                total: 0,
                token: '',
                //控制添加货物对话框的显示与隐藏
                addDialogVisible: false,
                //添加货物的表单数据
                addGood: {
                    goodsName: '',
                    goodsNum: '',
                    price: '',
                    description: '',
                    manufacturerId: ''
                },
                //添加货物的验证规则对象
                addGoodRules: {
                    goodsName: [
                        {required: true, message: '请输入货物名称', trigger: 'blur'}
                    ],
                    goodsNum: [
                        {required: true, message: '请输入货物数量', trigger: 'blur'},
                        {validator: checkNumber, trigger: 'blur'}
                    ],
                    price: [
                        {required: true, message: '请输入货物价格', trigger: 'blur'},
                        {validator: checkNumber, trigger: 'blur'}
                    ]
                },
                //控制修改货物对话框的显示与隐藏
                editDialogVisible: false,
                //查询到的货物信息对象
                editGood: {},
                //修改表单验证规则对象
                editGoodRules: {
                    goodsName: [
                        {required: true, message: '请输入货物名称', trigger: 'blur'}
                    ],
                    goodsNum: [
                        {required: true, message: '请输入货物数量', trigger: 'blur'},
                        {validator: checkNumber, trigger: 'blur'}
                    ],
                    price: [
                        {required: true, message: '请输入货物价格', trigger: 'blur'},
                        {validator: checkNumber, trigger: 'blur'}
                    ]
                }
            }
        },
        async created() {
            this.token = window.sessionStorage.getItem("token")
            var manufacturer = await this.$http.get('/user/manufacturer/getInformationFromToken', {
                params: {
                    token: this.token
                }
            })
            this.queryInfo.manufacturerId = manufacturer.data.data.manufacturerId
            this.getGoodsList()
        },
        methods: {
            async getGoodsList() {
                var result = await this.$http.get('goods/selectAllByManufacturerId', {params: this.queryInfo})
                if (result.data.status !== 200) {
                    return this.$message.error("获取货物列表失败！")
                }
                this.goodsList = result.data.data.records
                this.total = result.data.data.total
            },
            //监听pagesize改变的事件
            handleSizeChange(newSize) {
                this.queryInfo.pageSize = newSize
                this.getGoodsList()
            },
            //监听页码值改变的事件
            handleCurrentChange(newPage) {
                this.queryInfo.current = newPage
                this.getGoodsList()
            },
            //监听添加货物对话框的关闭事件
            addDialogClosed() {
                this.$refs.addGoodRef.resetFields()
            },
            //点击添加厂家
            addOneGood() {
                this.$refs.addGoodRef.validate(async valid => {
                    if (!valid) return
                    this.addGood.manufacturerId = this.queryInfo.manufacturerId
                    //发起网络请求
                    const result = await this.$http.post('goods/add', this.addGood)
                    if (result.data.status !== 200) {
                        this.$message.error('添加货物失败！')
                        return
                    }
                    this.$message.success('添加货物成功！')
                    // 隐藏添加货物的对话框
                    this.addDialogVisible = false
                    // 重新获取货物列表数据
                    this.getGoodsList()
                })
            },
            //展示编辑货物的对话框
            async showEditDialog(goodsId) {
                const result = await this.$http.get('goods/selectByGoodsId', {
                    params: {
                        goodsId: goodsId
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error('查询货物信息失败！')
                }
                this.editGood = result.data.data
                this.editDialogVisible = true
            },
            //监听修改货物对话框的关闭事件
            editDialogClosed() {
                this.$refs.editGoodRef.resetFields()
            },
            //修改货物信息并提交
            editOneGood() {
                this.$refs.editGoodRef.validate(async valid => {
                    if (!valid) return
                    // 发起修改货物的数据请求
                    const result = await this.$http.put('goods/update', this.editGood)
                    if (result.data.status !== 200) {
                        return this.$message.error('更新货物信息失败！')
                    }
                    // 关闭对话框
                    this.editDialogVisible = false
                    // 刷新数据列表
                    this.getGoodsList()
                    // 提示修改成功
                    this.$message.success('更新货物信息成功！')
                })
            },
            //根据账号删除货物
            async removeManufacturer(goodsId) {
                //弹框是否删除
                const confirmResult = await this.$confirm('此操作将永久删除该货物, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(error => error)
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消删除')
                }
                const result = await this.$http.delete('goods/delete', {
                    params: {
                        goodsId: goodsId
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error('删除货物失败！')
                }
                this.$message.success('删除货物成功！')
                this.getGoodsList()
            }
        }
    }
</script>

<style scoped>
    .el-breadcrumb {
        margin-bottom: 15px;
        font-size: 12px;
    }

    .el-card {
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
    }

    .el-table {
        margin-top: 15px;
        font-size: 12px;
    }

    .el-pagination {
        text-align: right;
        margin-top: 15px;
    }

</style>
